﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GrouponDesktop.AbmProveedor
{
    public partial class ModifProv : Form
    {

        private int idProv;

        public ModifProv(int id)
        {
            this.idProv = id;
            InitializeComponent();
            BDConnection.FillComboBox(cb_ciudad, "select ciudad from CodingSpree.TL_CIUDADES");
            BDConnection.FillComboBox(cb_rubro, "select rubro from CodingSpree.TL_RUBROS");

            string text = BDConnection.QueryAndExecuteScalarString("select razon_social from CodingSpree.TL_PROVEEDORES where id_proveedores = " + this.idProv);
            tb_rs.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select dominio from CodingSpree.TL_PROVEEDORES where id_proveedores = " + this.idProv);
            tb_dom.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select RUB.rubro from CodingSpree.TL_PROVEEDORES PRO, CodingSpree.TL_RUBROS RUB where RUB.id_rubro = PRO.id_rubro and id_proveedores = " + this.idProv);
            cb_rubro.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select CUIT from CodingSpree.TL_PROVEEDORES where id_proveedores = " + this.idProv);
            tb_cuit.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select telefono from CodingSpree.TL_PROVEEDORES where id_proveedores = " + this.idProv);
            tb_tel.Text = text;

            text = BDConnection.QueryAndExecuteScalarString("select b.ciudad from CodingSpree.TL_PROVEEDORES a, CodingSpree.TL_CIUDADES b where a.id_ciudad = b.id_ciudad and id_proveedores = " + this.idProv);
            cb_ciudad.Text = text;
        
        }

        private void b_modif_Click(object sender, EventArgs e)
        {
            string query = "EXEC CodingSpree.existeProv '" + tb_rs.Text + "', '" + tb_rs.Text +"'";
            string result = BDConnection.QueryAndExecuteScalarString(query);


            if (result == "1")
            { //VERIFICO SI YA EXISTE OTRO PROVEEDOR CON LOS MISMO DATOS.

                query = "EXEC CodingSpree.updateProv ";
                query = query + idProv + ", '";
                query = query + tb_rs.Text + "', '";
                query = query + tb_dom.Text + "', '";
                query = query + cb_ciudad.Text + "', ";
                query = query + tb_tel.Text + ", '";
                query = query + tb_cuit.Text + "', '";
                query = query + cb_rubro.Text + "'";

                BDConnection.execute(query);

                if (cb_habil.Checked == true)
                {
                    BDConnection.execute("EXEC CodingSpree.altaLogProv " + idProv);
                }

                MessageBox.Show("Modificacion efectuada");

            }
            else
            {


            }
        }

        private void b_pass_Click(object sender, EventArgs e)
        {
            string user = BDConnection.QueryAndExecuteScalarString("EXEC CodingSpree.getUserProv " + idProv);
            CambiarContraseña CC = new CambiarContraseña(user);
            CC.Show();
        }

        




    }
}
